<!doctype html>
<html>

	<head>
		<meta charset="utf-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="css/mui.css" rel="stylesheet" />
		<style>
			html,
			body {
				height: 100%;
			}
			
			.mui-content {
				height: 100%;
				overflow: auto;
			}
			
			.red-point{
			  position: relative;
			}
	
			.red-point::before{
			  content: " ";
			  border: 5px solid #C9394A;/*设置红色*/
			  border-radius:5px;/*设置圆角*/
			  position: absolute;
			  z-index: 3000;
			  right: 0%;
			  margin-right: -10px;
			  margin-top: 0px;
			}
		</style>
	</head>

	<body>

		<div class="mui-content">
			<ul class="mui-table-view" id="ul_friend_request_list" style="margin-bottom: 10px;">

			</ul>
			
			<ul id="ul_chatSnapshot" class="mui-table-view">
				
			</ul>
		</div>

		<script src="js/app.js"></script>
		<script src="js/mui.js"></script>
		<script type="text/javascript">
			mui.init();

			mui.plusReady(function() {
				// 获取用户全局对象
				var user = app.getUserGlobalInfo();

				// 加载好友请求记录
				var thisWebview = plus.webview.currentWebview();
				thisWebview.addEventListener("show", function() {
					loadingFriendRequests();
				})

				// 添加自定义事件,刷新好友,初始化websocket
				window.addEventListener("refresh", function() {
					loadingFriendRequests();
					CHAT.init();
				});
				//CHAT.init();
				// 渲染快照列表进行展示
				loadingChatSnapshot();
				
				// 为好友通讯录批量绑定点击事件
				mui("#ul_chatSnapshot").on("tap", ".chat-with-friend", function(e){
					var friendUserId = this.getAttribute("friendUserId");
					var friendNickname = this.getAttribute("friendNickname");
					var friendFaceImage = this.getAttribute("friendFaceImage");
					
					
					// 打开聊天子页面
					mui.openWindow({
						url: "chatting.html",
						id: "chatting-" + friendUserId,	// 每个朋友的聊天页面都是唯一对应的
						extras: {
							friendUserId: friendUserId,
							friendNickname: friendNickname,
							friendFaceImage: friendFaceImage
						}
					});
					
					// 标记未读状态为已读
					app.readUserChatSnapshot(user.id,friendUserId);
					// 渲染快照列表进行展示
					loadingChatSnapshot();
				})
				
				// 左滑删除聊天快照和记录
				mui("#ul_chatSnapshot").on("tap","#link_delChatRecord",function(e){
					var me = this;	// a标签
					
					// 获取朋友id
					var friendUserId = me.getAttribute("friendUserId");
					
					// 1.删除本地聊天记录
					app.deleteUserChatHistory(user.id,friendUserId);
					// 2.删除本地聊天记录
					app.deleteUserChatSnapshot(user.id,friendUserId);
					// 3.删除当前用户操作的dom节点
					var li = me.parentNode.parentNode;	// 获取li标签
					// 删除 li 标签
					var ul_chatSnapshot = document.getElementById("ul_chatSnapshot");
					ul_chatSnapshot.removeChild(li);
				})
				
			})
			
			// 构造聊天业务的CHAT
			window.CHAT = {
				socket: null,
				init: function() {
					if (window.WebSocket) {
						CHAT.socket = new WebSocket(app.nettyServerUrl);
						CHAT.socket.onopen = CHAT.wsopen,
							CHAT.socket.onclose = CHAT.wsclose,
							CHAT.socket.onerror = CHAT.wserror,
							CHAT.socket.onmessage = CHAT.wsmessage
					} else {
						alert("手机设备过旧,请升级手机设备...")
					}
					// 渲染快照列表进行展示
					loadingChatSnapshot();
					
				},
				chat: function(msg) {
					// 如果当前websocket的状态是已打开，则直接发送， 否则重连
					if (CHAT.socket != null 
							&& CHAT.socket != undefined  
							&& CHAT.socket.readyState == WebSocket.OPEN) {
							CHAT.socket.send(msg);
					} else {
						// 重连websocket
						CHAT.init();
						setTimeout("CHAT.reChat('" + msg + "')", "1000");
					}
					// 渲染快照列表进行展示
					loadingChatSnapshot();
				},
				reChat: function(msg) {
					console.log("消息重新发送...");
					CHAT.socket.send(msg);
				},
				wsopen: function() {
					console.log("webscoket连接已建立...");

					var me = app.getUserGlobalInfo();
					// 构造 ChatMsg
					var chatMsg = new app.ChatMsg(me.id, null, null, null);
					// 构造 DataContent
					var dataContent = new app.DataContent(app.CONNECT, chatMsg, null);
					// 发送webscoket
					CHAT.chat(JSON.stringify(dataContent));
					
					// 每次连接之后,获取用户的未读未签收消息列表
					fetchUnReadMsg();
					
					
					// 定时发送心跳
					var keep = setInterval(()=>{
						var connectionStatus = plus.networkinfo.getCurrentType();
						if(connectionStatus <= 1){
							clearInterval(keep)
						}else{
							CHAT.keepalive()
						}
					}, 10000);
					
					// 网络重连初始化
					document.addEventListener("netchange", function() {
						var connectionStatus = plus.networkinfo.getCurrentType();
						if (connectionStatus != 0 && connectionStatus != 1) {
							CHAT.init();
						} 
					});
				},
				wsclose: function() {
					console.log("连接关闭...");
				},
				wserror: function() {
					console.log("连接错误...");
				},
				wsmessage: function(e) {
					console.log("接受到消息：" + e.data);

					// 转换DataContent为对象
					var dataContent = JSON.parse(e.data)
					var action = dataContent.action;

					if (action === app.PULL_FRIEND) {
						fetchContactList();
						return false;
					}

					// 如果不是重新拉去好友列表,则获取聊天消息模型,渲染接受到的聊天记录
					var chatMsg = dataContent.chatMsg;
					var msg = chatMsg.msg;
					var friendUsersId = chatMsg.senderId;
					var myId = chatMsg.receiverId;
					
					console.log(JSON.stringify(chatMsg))

					// 调用聊天webview的receiveMsg方法
					//var chatWebview = plus.webview.getWebviewById("chatting-2004167F9P6KKKAW");
					var chatWebview = plus.webview.getWebviewById("chatting-" + friendUsersId);
					var isRead = true;
					if (chatWebview != null) {
						chatWebview.evalJS("receiveMsg('" + msg + "')");
						chatWebview.evalJS("resizeScreen()");
					} else {
						isRead = false;	// chatWebview 聊天页面没有打开,标记消息未读状态
					}

					// 接受到消息之后，对消息记录进行签收
					var dataContentSign = new app.DataContent(app.SIGNED, null, chatMsg.msgId);
					CHAT.chat(JSON.stringify(dataContentSign));

					// 保存聊天历史记录到本地缓存
					app.saveUserChatHistory(myId, friendUsersId, msg, 2);
					app.saveUserChatSnapshot(myId, friendUsersId, msg, isRead);
					// 渲染快照列表进行展示
					loadingChatSnapshot();
				},
				signMsgList: function(unSignedMsgIds) {
					// 构建批量签收对象的模型
					var dataContentSign = new app.DataContent(app.SIGNED,null,unSignedMsgIds);
					// 发送批量签收的请求
					CHAT.chat(JSON.stringify(dataContentSign));
				},
				keepalive: function() {
						// 构建对象
						var dataContent = new app.DataContent(app.KEEPALIVE, null, null);
						// 发送心跳
						CHAT.chat(JSON.stringify(dataContent));
						
						// 定时执行函数
						fetchUnReadMsg();
						fetchContactList();
					}
			}
			
			// 每次重连后获取服务器的未签收消息
			function fetchUnReadMsg() {
				var user = app.getUserGlobalInfo();
				var msgIds = ",";	// 格式：  ,1001,1002,1003,
				mui.ajax(app.serverUrl + "/u/getUnReadMsgList?acceptUserId=" + user.id,{
					data:{},
					dataType:'json',//服务器返回json格式数据
					type:'post',//HTTP请求类型
					timeout:10000,//超时时间设置为10秒；
					headers:{'Content-Type':'application/json'},	              
					success:function(data){
						
						if (data.status == 200) {
							var unReadMsgList = data.data;
							
							// 1. 保存聊天记录到本地
							// 2. 保存聊天快照到本地
							// 3. 对这些未签收的消息，批量签收
							for (var i = 0 ; i < unReadMsgList.length ; i ++) {
								var msgObj = unReadMsgList[i];
								// 逐条存入聊天记录
								app.saveUserChatHistory(msgObj.acceptUserId, 
														msgObj.sendUserId, 
														msgObj.msg, 2);
								// 存入聊天快照
								app.saveUserChatSnapshot(msgObj.acceptUserId, 
														 msgObj.sendUserId, 
														 msgObj.msg, false);
								// 拼接批量接受的消息id字符串，逗号间隔
								msgIds += msgObj.id;
								msgIds += ",";
							}
							
							// 调用批量签收的方法
							CHAT.signMsgList(msgIds);
							// 刷新快照
							loadingChatSnapshot();
						}
					}
				});
			}
			
			// 获取后端所有好友列表
			function fetchContactList() {
				var user = app.getUserGlobalInfo();
				mui.ajax(app.serverUrl + "/u/myFriends?userId=" + user.id, {
					data: {},
					dataType: 'json', //服务器返回json格式数据
					type: 'post', //HTTP请求类型
					timeout: 10000, //超时时间设置为10秒；
					headers: {
						'Content-Type': 'application/json'
					},
					success: function(data) {
			
						if (data.status == 200) {
							var contactList = data.data;
							plus.storage.removeItem("contactList");
							app.setContactList(contactList);
						}
					}
				});
			}
			
			// 展示聊天快照,渲染列表
			function loadingChatSnapshot(){
				// 获取用户全局对象
				var user = app.getUserGlobalInfo();
				var chatSnapshotList = app.getUserChatSnapshot(user.id);
				
				var chatItemHtml = "";
				var ul_chatSnapshot = document.getElementById("ul_chatSnapshot");
				ul_chatSnapshot.innerHTML = "";
				for(var i = 0;i<chatSnapshotList.length;i++){
					var chatItem  = chatSnapshotList[i];
					
					var friendId = chatItem.friendId;
					// 根据friendId从本地联系人列表的缓存中拿到相关信息
					var firend = app.getFriendFromContactList(friendId);
					
					// 判断消息的已读或未读状态
					var isRead  = chatItem.isRead;
					var readHtmlBefore = '';
					var readHtmlAfter = '';
					if(!isRead){
						readHtmlBefore = '<span class="red-point">';
						readHtmlAfter = '</span>';
					}
					
					chatHistoryList =  
					'<li class="mui-table-view-cell mui-media chat-with-friend" friendUserId="'+friendId+'" friendNickname="'+firend.friendNickname+'" friendFaceImage="'+firend.friendFaceImage+'" >'+
						'<div class="mui-slider-right mui-disabled">'+
							'<a id="link_delChatRecord" friendUserId="'+friendId+'" class="mui-btn mui-btn-red">删除</a>'+
						'</div>'+
						'<div class="mui-media-body mui-slider-handle">'+
							'<img class="mui-media-object mui-pull-left" src="'+firend.friendFaceImage+'">'+
							readHtmlBefore + firend.friendNickname + readHtmlAfter +
							'<p class="mui-ellipsis">'+chatItem.msg+'</p>'+
						'</div>'+
					'</li>';
					ul_chatSnapshot.insertAdjacentHTML('beforeend',chatHistoryList);
				}
			}
			
			

			// 加载好友请求列表
			function loadingFriendRequests() {
				// 获取用户全局对象
				var user = app.getUserGlobalInfo();
				mui.ajax(app.serverUrl + "/u/queryFriendRequest?userId=" + user.id, {
					data: {},
					dataType: 'json', //服务器返回json格式数据
					type: 'post', //HTTP请求类型
					timeout: 10000, //超时时间设置为10秒；
					headers: {
						'Content-Type': 'application/json'
					},
					success: function(data) {
						//服务器返回响应，根据响应结果
						if (data.status == 200) {
							var friendRequestList = data.data;

							var ul_friend_request_list = document.getElementById("ul_friend_request_list");
							if (friendRequestList != null && friendRequestList.length > 0) {

								var requestHtml = "";
								for (var i = 0; i < friendRequestList.length; i++) {
									requestHtml += renderFriendRequests(friendRequestList[i]);
								}
								ul_friend_request_list.innerHTML = requestHtml;

								// 动态对忽略和通过按钮进行事件绑定
								mui(".btnOper").on("tap", ".ignoreRequest", function(e) {
									var friendId = this.getAttribute("friendId");
									operatorFriendRequest(user.id, friendId, 0);
								});

								mui(".btnOper").on("tap", ".passRequest", function(e) {
									var friendId = this.getAttribute("friendId");
									operatorFriendRequest(user.id, friendId, 1);
								});
							} else {
								ul_friend_request_list.innerHTML = "";
							}
						}
					}
				})
			}

			// 操作好友请求
			function operatorFriendRequest(userId, friendId, operType) {
				mui.ajax(app.serverUrl + "/u/openFriendRequest?acceptUserId=" + userId + "&sendUserId=" + friendId + "&operType=" +
					operType, {
						data: {},
						dataType: 'json', //服务器返回json格式数据
						type: 'post', //HTTP请求类型
						timeout: 10000, //超时时间设置为10秒；
						headers: {
							'Content-Type': 'application/json'
						},
						success: function(data) {
							// 更新通讯录
							var myFriendList = data.data;
							app.setContactList();

							// 重新加载好友请求记录
							loadingFriendRequests();
						}
					})
			}

			// 用于拼接单个朋友的请求
			function renderFriendRequests(friend) {
				var html = "";

				html = '<li class="btnOper mui-table-view-cell mui-media">' +
					'<a href="javascript:;">' +
					'<img class="mui-media-object mui-pull-left" src="' + friend.sendFaceImage + '">' +
					'<span id="span_nickname" class="mui-pull-right" style="margin-top:8px;">' +
					'<button friendId="' + friend.sendUserId +
					'" type="button" class="ignoreRequest mui-btn mui-btn-grey" style="padding: 4px 10px;margin-right:5px;">忽略</button>' +
					'<button friendId="' + friend.sendUserId +
					'" type="button" class="passRequest mui-btn mui-btn-danger" style="padding: 4px 10px;">通过</button>' +
					'</span>' +
					'<div class="mui-media-body">' +
					'<label>' + friend.sendNickname + '</label>' +
					'<p class="mui-ellipsis">请求添加你为朋友</p>' +
					'</div>' +
					'</a>' +
					'</li>';

				return html;
			}
		</script>
	</body>

</html>
